<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: AdminManageController.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_AdminManageController extends Core_Controller_Action_Admin {
	
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'vote_admin_main', array (), 'vote_admin_main_manage' );
		
		$page = $this->_getParam ( 'page', 1 );
		
		$table = $this->_helper->api ()->getDbtable ( 'challenges', 'vote' );
		$select = $table->select ();
		
		$this->view->form = $form = new Vote_Form_Admin_Manage_Filter ();
		
		$values = array ();
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
		}
		
		foreach ( $values as $key => $value ) {
			if (null === $value) {
				unset ( $values [$key] );
			}
		}
		
		$values = array_merge ( array ('order' => 'challenge_id', 'order_direction' => 'DESC' ), $values );
		
		$this->view->assign ( $values );
		
		$select->order ( (! empty ( $values ['order'] ) ? $values ['order'] : 'challenge_id') . ' ' . (! empty ( $values ['order_direction'] ) ? $values ['order_direction'] : 'DESC') );
		
		if (! empty ( $values ['keyword'] )) {
			$select->where ( 'title LIKE ?', '%' . $values ['keyword'] . '%' );
		}
		
		if (isset ( $values ['approved'] ) && $values ['approved'] > - 1) {
			$select->where ( 'approved = ?', ( int ) $values ['approved'] );
		}
		
		if (isset ( $values ['featured'] ) && $values ['featured'] > - 1) {
			$select->where ( 'featured = ?', ( int ) $values ['featured'] );
		}
		
		if (isset ( $values ['type'] ) && $values ['type']) {
			$select->where ( 'type = ?', $values ['type'] );
		}
		
		$this->view->paginator = Zend_Paginator::factory ( $select );
		$this->view->paginator->setItemCountPerPage ( 25 );
		$this->view->paginator->setCurrentPageNumber ( $page );
		
		if ($this->getRequest ()->isPost ()) {
			$values = $this->getRequest ()->getPost ();
			foreach ( $values as $key => $value ) {
				if ($key == 'delete_' . $value) {
					$voting = Engine_Api::_ ()->getItem ( 'vote_challenge', $value );
					$voting->delete ();
				}
			}
		}
	}
	
	public function multiModifyAction() {
		$contestIds = array_values ( $this->_getParam ( 'modify' ) );
		$table = Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' );
		$select = $table->select ()->where ( 'challenge_id IN (?)', $contestIds );
		
		$contests = $table->fetchAll ( $select );
		foreach ( $contests as $contest ) {
			$contest->delete ();
		}
		
		return $this->_helper->redirector->gotoRoute ( array ('action' => 'index', 'controller' => 'manage', 'module' => 'vote' ), 'admin_default', true );
	}
	
	public function deleteAction() {
		$id = $this->_getParam ( 'challenge_id' );
		$this->view->contest_id = $id;
		
		$this->view->form = $form = new Vote_Form_Delete ();
		
		// Check post
		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$contest = Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $id )->current ();
				$contest->delete ();
				
				$db->commit ();
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('Voting was deleted.' ) ) );
		}
	}
	
	public function featuredAction() {
		$id = $this->_getParam ( 'challenge_id' );
		$featured = ( int ) $this->_getParam ( 'featured' );
		$this->view->contest_id = $id;
		
		$db = Engine_Db_Table::getDefaultAdapter ();
		$db->beginTransaction ();
		
		try {
			$contest = Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $id )->current ();
			$contest->featured = $featured;
			$contest->save ();
			
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		return $this->_helper->redirector->gotoRoute ( array ('action' => 'index', 'controller' => 'manage', 'module' => 'vote' ), 'admin_default', true );
	}
	
	public function approveAction() {
		$id = $this->_getParam ( 'challenge_id' );
		$approved = ( int ) $this->_getParam ( 'approve' );
		$this->view->contest_id = $id;
		
		$db = Engine_Db_Table::getDefaultAdapter ();
		$db->beginTransaction ();
		
		try {
			$contest = Engine_Api::_ ()->getDbTable ( 'challenges', 'vote' )->find ( $id )->current ();
			$contest->approved = $approved;
			$contest->save ();
			
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		return $this->_helper->redirector->gotoRoute ( array ('action' => 'index', 'controller' => 'manage', 'module' => 'vote' ), 'admin_default', true );
	}
}